08. Dates and Calendar

Dates and Calendar

ND079 C1 L4 A08 Dates And Calendar

We will look at two Java classes that can be used to handle dates: the Date class and the Calendar class.

The Date Class

The Date class represents a specific instance in time. We can instantiate a new Date object like so:

Date date = new Date();

The Calendar Class

The Calendar class is an abstract class that provides methods for manipulating date and time. The basic syntax for instantiating a new Calendar object looks like this:

Calendar calendar = Calendar.getInstance();

QUIZ QUESTION::

For each of the descriptions below, does it better describe the Date class or the Calendar class?

ANSWER CHOICES:



Description

Class

Represents only a specific day and time.

Represents a specific day and time and provides transformation methods.

Has methods to set a specific Day, Month and Year.

Is an abstract class

Is a concrete class

SOLUTION:

Description

Class

Represents a specific day and time and provides transformation methods.

Has methods to set a specific Day, Month and Year.

Is an abstract class

Represents a specific day and time and provides transformation methods.

Has methods to set a specific Day, Month and Year.

Is an abstract class

Represents only a specific day and time.

Is a concrete class

Represents a specific day and time and provides transformation methods.

Has methods to set a specific Day, Month and Year.

Is an abstract class

Represents only a specific day and time.

Is a concrete class

Additional Resources

Java has some other classes—LocalDate, LocalTime, and LocalDateTime—that can be used to manipulate dates. These won't be needed for this course, but if you're curious, you can read about them in the official documentation here.